<?php

require_once 'config.php';
require_once 'common.php';
require_once 'ResultData.php';
require_once "Course.php";


$result = new ResultData();

$size = trim($_REQUEST['size']); // 一页的数量

if ($size == "") {
    $result->code = 404;
    $result->msg = "size未填写";
    exit(json_encode($result));
}
$size = intval($size);
$page = trim($_REQUEST['page']); // 第几页的数量

if ($page == "") {
    $result->code = 404;
    $result->msg = "page未填写";
    $json_string = json_encode($result);
    exit($json_string);
}
$page = intval($page);


$con = mysqli_connect($servername, $username, $password, $database, $port);
if (!$con) {
    $result->code = 404;
    $result->msg = "数据库连接不上";
    exit(json_encode($result));
}

$sql = "SELECT id, course_name, course_no FROM t_course LIMIT " . ($page - 1) * $size . "," . $size;
$sqlCount = "SELECT COUNT(*) count FROM t_course";

$record = $con->query($sql);

$list = array();

if ($record->num_rows > 0) {
    while ($row = $record->fetch_assoc()) {
        $course = new Course();
        $course->id = $row["id"];
        $course->courseName = $row["course_name"];
        $course->courseNo = $row["course_no"];
        array_push($list, $course);
    }
}

class Data
{
    public $count;
    public $list;
}

$data = new Data();
$data->list = $list;
$record = $con->query($sqlCount);
if ($record->num_rows > 0) {
    while ($row = $record->fetch_assoc()) {
        $data->count = $row['count'];
    }
}
mysqli_close($con);
$result->data = $data;

exit(json_encode($result));